package com.genghis.ptas.study;

import com.genghis.ptas.security.entity.User;
import com.genghis.ptas.study.entity.StudyRecord;
import com.genghis.ptas.study.service.StudyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Controller
@Transactional
@RequestMapping("/study/")
public class StudyController {
    @Autowired
    private StudyRecordService studyRecordService;

    @RequestMapping(value = "addStudyRecord", method = {RequestMethod.POST})
    @ResponseBody
    public void addStudyRecord(HttpServletRequest request, StudyRecord studyRecord) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        studyRecord.setUserId(user.getId());
        studyRecordService.addStudyRecord(studyRecord);
    }

    @RequestMapping(value = "findStudyRecordList", method = {RequestMethod.POST})
    @ResponseBody
    public void findStudyRecordList(StudyRecord studyRecord) {
        studyRecordService.findStudyRecordList(studyRecord);
    }

    @RequestMapping(value = "myStudyDo", method = {RequestMethod.GET})
    public String myStudyDo(HttpServletRequest request, StudyRecord studyRecord) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        studyRecord.setUserId(user.getId());
        List<StudyRecord> studyRecordList = studyRecordService.findStudyRecordList(studyRecord);
        request.setAttribute("studyRecordList", studyRecordList);
        return "frontstage/person/myStudyDo";
    }
}
